<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Category;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class ArticleController extends commencontroller
{
    ////GET.Admin/article   全部文章列表
          function index(){
            $artresoult=DB::table('blog_article')
                ->orderBy('art_id','desc')
                ->paginate(5);


        return view('Admin.article.index',compact('artresoult'));
    }
    //GET|HEAD.Admin/article/create添加文章
        function create (){

            $addresoult=(new Category)->tree();





        return view('Admin.article.add',compact('addresoult'));
    }

    //post. Admin/article
    function store (){
        $input=Input::except('_token');
        dd($input);
        $input['art_time']=time();
        $rules=['art_title'=>'required',

        ];
        $message=[
            'art_title.required'=>'文章列表不能为空',

        ];

        $validator=Validator::make($input,$rules,$message);

if($validator->passes()){
    $addcount=DB::table('blog_article')
        ->insert(
            $input
        );
    if ($addcount<=0){
        return back()->with('errors','添加文章失败，请稍后再试');
    }else{
       return redirect('Admin/article');

    }
}else{
    return back()->withErrors($validator);
}



    }
//修改
    //GET|HEAD.Admin/article/{index}/edit
    function edit ($art_id){
        $addresoult=(new Category)->tree();


   $upresoult=DB::table('blog_article')
    ->where('art_id',$art_id)
    ->get();





        return view('Admin/article/update',compact('upresoult','addresoult'));
    }



    //PUT|PATCH.Admin/article/{index
    function update($art_id){


        $up=Input::except('_method','_token');
        $up['art_time']=time();


        $upcount=  DB::table('blog_article')
            ->where('art_id',$art_id)
            ->update(
                $up
            );


        if ($upcount>0){
            return redirect('Admin/article');
        }else{
            return back()->withErrors('errors',修改失败);
        }

    }


//删除
    //DELETE Admin/index/{index}
    function destroy($art_id){

       $delcount=DB::table('blog_article')
            ->where('art_id',$art_id)
            ->delete();
        if ($delcount<=0){
            $update=[
                'status'=>0,
                'msg'=>'文章删除失败,请稍后再试',
            ];
        }else{
            $update=[
                'status'=>1,
                'msg'=>'文章删除成功',
            ];

        }
        return $update;
    }


}
